package com.android.lovingpet.base;


import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.lovingpet.R;


/**
 * Created by ikata on 2018/2/3.
 */

public abstract class BaseFragment<V extends BaseView,P extends BasePresenter<V>> extends Fragment implements BaseView {

    private View progress;
    private V view;
    private P presenter;

    public P getPresenter() {
        return presenter;
    }

    @LayoutRes
    protected abstract int setContentView();

    protected abstract void onCreateView(View view);

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (this.presenter==null)
        {
            this.presenter=createPresenter();
        }
        if(this.view==null)
        {
            this.view=createView();
        }
        if(this.view!=null&&this.presenter!=null)
        {
            this.presenter.attachView(this.view);
        }
    }

    public void reBind() {
        this.presenter=createPresenter();
        this.view=createView();
        this.presenter.attachView(this.view);
    }

    public void setTitle(View view,String title)
    {
        RelativeLayout relativeLayout=view.findViewById(R.id.rl_title);
        if (relativeLayout!=null)
        {
            TextView textView=relativeLayout.findViewById(R.id.tv_title);
            textView.setText(title);
        }
    }

    @Nullable
    @Override
    public final View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(setContentView(), container, false);
        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            progress = inflater.inflate(R.layout.loading, viewGroup, false);
            if (progress != null) {
                viewGroup.addView(progress);
                progress.bringToFront();
                progress.setOnClickListener(null);
            }
        }
        View back = view.findViewById(R.id.bt_back);
        if (back != null) {
            back.setOnClickListener(v -> {
                if (getFragmentManager().getBackStackEntryCount() == 0) {
                    getActivity().finish();
                } else {
                    getFragmentManager().popBackStack();
                }
            });
        }
        onCreateView(view);
        return view;
    }

    public abstract P createPresenter();
    public abstract V createView();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(this.presenter!=null&&this.view!=null)
        {
            this.presenter.detachView();
        }
    }

    @Override
    public void showToast(final String meg) {
        runOnUiThread(() -> Toast.makeText(getContext(), meg, Toast.LENGTH_SHORT).show());
    }

    @Override
    public void showProgress() {
        runOnUiThread(() -> {
            if (progress != null) {
                progress.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void hideProgress() {
        runOnUiThread(() -> {
            if (progress != null) {
                progress.setVisibility(View.GONE);
            }
        });
    }

    public final void runOnUiThread(Runnable action) {
        if (getActivity() != null) {
            getActivity().runOnUiThread(action);
        }
    }
}
